package 剑指offer;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @ProjectName leetcode
 * @Author Ding
 * @CreateTime 2022/8/25 15:58 星期四
 */
public class _35_复杂链表的复制 {

    public static void main(String[] args) {
        Node n1 = new Node(7);
        Node n2 = new Node(13);
        Node n3 = new Node(11);
        Node n4 = new Node(10);
        Node n5 = new Node(1);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;

        n1.random = null;
        n2.random = n1;
        n3.random = n5;
        n4.random = n3;
        n5.random = n1;

        Node n = copyRandomList(n1);
        while (n != null){
            System.err.println(n.val);
            n = n.next;
        }
    }


    public static Node copyRandomList(Node head) {
        Node p = head;
        Node copyObj = new Node(p.val);
        while (p!=null){
            copyObj = new Node(p.val);
            copyObj.next = p.next;
            copyObj.random = p.random;
            p = p.next;
        }
        return copyObj;
    }
    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
}
